package com.changgou.seckill.mq;

import com.alibaba.fastjson.JSON;
import com.changgou.seckill.service.SeckillOrderService;
import com.github.wxpay.sdk.WXPayUtil;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * 秒杀商品订单监听
 */
@Component
@RabbitListener(queues = "${mq.pay.queue.seckillqueue}")
public class seckillMessageListener {
    @Autowired
    private SeckillOrderService seckillOrderService;
    @RabbitHandler
    public void getMessage(String message){
        try {
            //将支付信息转成map
            Map<String, String> resultMap = JSON.parseObject(message,Map.class);
            String return_code = resultMap.get("return_code");
            //微信订单号
            String out_trade_no = resultMap.get("out_trade_no");
            //自定义数据
            String attach = resultMap.get("attach");
            Map<String,String> attachMap = JSON.parseObject(attach, Map.class);
            String username = attachMap.get("username");
            //支付成功
            if ("SUCCESS".equals(return_code)){
                //获取支付结果
                String result_code = resultMap.get("result_code");
                if (result_code.equals("SUCCESS")){
                    //成功支付
                    seckillOrderService.updatePayStatus(resultMap.get("time_end"),resultMap.get("transaction_id\t"),username);
                }else {
                    seckillOrderService.closeOrder(username);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}
